package modules.cms.models
{
	import flash.utils.getQualifiedClassName;
	import devdoo.fx.UseDB;
	import devdoo.fx.UseUtil;
	import devdoo.fx.UseString;
	import devdoo.fx.interfaces.ITableModel;
	
	public class UseMenuDepartment implements ITableModel
	{
		private var _classNameModel:String = 'useMenuDepartment';
		private var _table:String = 'menu_department';
		private var _alias:String = 'USE_MENU_DEPARTMENT';
		private var _data:Object = new Object();
		private var _fields:Object = new Object();
		private var _idField:String = 'id';
		private var _idFieldAlias:String = 'USE_MENU_DEPARTMENT_id';
		private var _useTableAlias:Boolean;
		private var _isSave:Boolean = false;
		
		public function UseMenuDepartment(){
			this._fields.USE_MENU_DEPARTMENT_change_frequency = "change_frequency";
			this._fields.USE_MENU_DEPARTMENT_classname = "classname";
			this._fields.USE_MENU_DEPARTMENT_href = "href";
			this._fields.USE_MENU_DEPARTMENT_html_id = "html_id";
			this._fields.USE_MENU_DEPARTMENT_id = "id";
			this._fields.USE_MENU_DEPARTMENT_is_active = "is_active";
			this._fields.USE_MENU_DEPARTMENT_menu_id = "menu_id";
			this._fields.USE_MENU_DEPARTMENT_order_position = "order_position";
			this._fields.USE_MENU_DEPARTMENT_priority = "priority";
			this._fields.USE_MENU_DEPARTMENT_style = "style";
			this._fields.USE_MENU_DEPARTMENT_target = "target";
			this._fields.USE_MENU_DEPARTMENT_total_itens = "total_itens";
		}
		
		public function get tableAlias():Object{
			var table:Object = new Object();
			table[this._alias] = this._table;
			return table;
		}
		
		public function get classNameModel():String{
			return this._classNameModel;
		}
		
		public function get table():String{
			return this._table;
		}
		
		public function get idField():String{
			return this._idField;
		}
		
		public function get idFieldAlias():String{
			return this._idFieldAlias;
		}
		
		public function get alias():String{ return this._alias; }
		
		public function fields(fieldsList:Array, useTableAlias:Boolean=false):Object{
			var data:Object = new Object();
			var fields:Object = this._fields;
			var errorFields:String = '';
			var error:Boolean = true;
			var item:String;
			var field:String;
			var i:String;
			var j:String;
			
			this._useTableAlias = useTableAlias;
			if(typeof(fieldsList[0]) == 'string' && fieldsList[0] == '*' ){
				if(useTableAlias){
					for ( field in this._fields){
						data[field] = this._fields[field];
					}
				}
				else{
					for ( field in this._fields){
						data[(this._fields[field])] = this._fields[field];
					}
				}
				for (item in this._setVar){
					data[item] = this._setVar[item];
				}
				for (item in this._expr){
					data[item] = this._expr[item];
				}
				return data;
			}
			for (j in fieldsList){
				if(useTableAlias){
					for (i in this._fields){
						if( fields[i] == fieldsList[j] ){
							data[this._alias+'_'+fields[i]] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				else{
					for (i in fields){
						if(typeof(fieldsList[j])== 'object'){
							for (var k:String in fieldsList[j]) {
								data[k] = fieldsList[j][k];
								error = false;
								continue;
							}
						}
						else if( fields[i] == fieldsList[j] ){
							data[(fields[i])] = fieldsList[j];
							error = false;
							continue;
						}
					}
				}
				if(error) errorFields += fieldsList[j]+' :: ';
				error = true;
			}
			if(errorFields != ''){
				throw new Error(getQualifiedClassName(this) +'::Error( :: '+errorFields+')', 'Error :: Campos não encontrado no modelo da tabela '+this._table);
			}
			for (item in this._setVar){
				data[item] = this._setVar[item];
			}
			for (item in this._expr){
				data[item] = this._expr[item];
			}
			return data;
		}
		
		private var _expr:Object = new Object();
		public function expr(field:String, expr:String, alias:String = null, useTableAlias:Boolean=false):UseMenuDepartment{
			var fields:Object = this._fields;
			var error:Boolean = true;
			for (var i:String in fields){
				if( fields[i] == field ) error = false;
			}
			if(error){
				throw new Error(getQualifiedClassName(this) +'::Error exp('+field+')', 'Error :: Campo utilizado em exp(?) não encontrado no modelo da tabela '+this._table);
				return null;
			}
			if(alias == null){
				if(useTableAlias)
					alias = this._alias + '_' + field;
				else
					alias = field;
			}
			else{
				if(useTableAlias)
					alias =  this._alias + '_' + alias;
			}
			this._expr[(alias)] = UseString.replace(expr, '?', this._alias + '.' + field) +' as ' + alias;
			return this;
		}
		
		private var _setVar:Object = new Object();
		public function setVar(field:String, value:String):UseMenuDepartment{
			this._setVar[field] = value;
			return this;
		}
		public function get dataAlias():Object{
			var data:Object = {};
			data.USE_MENU_DEPARTMENT_change_frequency = this.change_frequency;
			data.USE_MENU_DEPARTMENT_classname = this.classname;
			data.USE_MENU_DEPARTMENT_href = this.href;
			data.USE_MENU_DEPARTMENT_html_id = this.html_id;
			data.USE_MENU_DEPARTMENT_id = this.id;
			data.USE_MENU_DEPARTMENT_is_active = this.is_active;
			data.USE_MENU_DEPARTMENT_menu_id = this.menu_id;
			data.USE_MENU_DEPARTMENT_order_position = this.order_position;
			data.USE_MENU_DEPARTMENT_priority = this.priority;
			data.USE_MENU_DEPARTMENT_style = this.style;
			data.USE_MENU_DEPARTMENT_target = this.target;
			data.USE_MENU_DEPARTMENT_total_itens = this.total_itens;
			return data;
		}
		public function get data():Object{
			var data:Object = {};
			data.change_frequency = this.change_frequency;
			data.classname = this.classname;
			data.href = this.href;
			data.html_id = this.html_id;
			data.id = this.id;
			data.is_active = this.is_active;
			data.menu_id = this.menu_id;
			data.order_position = this.order_position;
			data.priority = this.priority;
			data.style = this.style;
			data.target = this.target;
			data.total_itens = this.total_itens;
			return data;
		}
		public function set data(data:Object):void{
			var len:int = this._alias.length+1;
			var alias:String;
			var tempIndex:String;
			for (var index:* in data){
				alias = index.substring(0,len);
				if(alias == this._alias+'_'){
					tempIndex = index.replace(alias,'');
				}
				if(this.hasOwnProperty(tempIndex)){
					this[tempIndex] = data[index];
				}
			}
		}
		
		public function find(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
				.from(tableAlias)
				.fields(['*'])
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.fetch('All')
				.connect(callBack);
		}
		
		public function remove(id:int, callBack:Function):void{
			var useDB:UseDB = new UseDB('DELETE');
			useDB.setFetchMode(UseDB.FETCH_OBJ)
			useDB.table(table)
				.where(this._alias +'.'+ this.idField + ' = ?', String(id))
				.connect(callBack);
		}
		
		public function update(callBack:Function):void{
			var useDB:UseDB = new UseDB('UPDATE');
			useDB.table(table)
				.data(this._data)
				.where(this.idField + ' = ?', this.id)
				.connect(callBack);
		}
		
		public function insert(callBack:Function):void{
			var useDB:UseDB = new UseDB('INSERT');
			useDB.table(table)
				.data(this._data)
				.connect(callBack);
		}
		
		public function fetch(callBack:Function, type:String = 'All'):void{
			var useDB:UseDB = new UseDB('SELECT');
			useDB.from(tableAlias)
				.fields(['*'])
				.fetch(type)
				.connect(callBack);
		}
		
		private var _change_frequency:*;
		public function set change_frequency(change_frequency:*):void{
			this._change_frequency = UseUtil.checkSetData('change_frequency', change_frequency);
			this._data['change_frequency'] = change_frequency;
			this._isSave = true;
		}
		public function get change_frequency():*{ return this._change_frequency; }
		
		private var _classname:*;
		public function set classname(classname:*):void{
			this._classname = UseUtil.checkSetData('classname', classname);
			this._data['classname'] = classname;
			this._isSave = true;
		}
		public function get classname():*{ return this._classname; }
		
		private var _href:*;
		public function set href(href:*):void{
			this._href = UseUtil.checkSetData('href', href);
			this._data['href'] = href;
			this._isSave = true;
		}
		public function get href():*{ return this._href; }
		
		private var _html_id:*;
		public function set html_id(html_id:*):void{
			this._html_id = UseUtil.checkSetData('html_id', html_id);
			this._data['html_id'] = html_id;
			this._isSave = true;
		}
		public function get html_id():*{ return this._html_id; }
		
		private var _id:*;
		public function set id(id:*):void{
			this._id = UseUtil.checkSetData('id', id);
			this._data['id'] = id;
			this._isSave = true;
		}
		public function get id():*{ return this._id; }
		
		private var _is_active:*;
		public function set is_active(is_active:*):void{
			this._is_active = UseUtil.checkSetData('is_active', is_active);
			this._data['is_active'] = is_active;
			this._isSave = true;
		}
		public function get is_active():*{ return this._is_active; }
		
		private var _menu_id:*;
		public function set menu_id(menu_id:*):void{
			this._menu_id = UseUtil.checkSetData('menu_id', menu_id);
			this._data['menu_id'] = menu_id;
			this._isSave = true;
		}
		public function get menu_id():*{ return this._menu_id; }
		
		private var _order_position:*;
		public function set order_position(order_position:*):void{
			this._order_position = UseUtil.checkSetData('order_position', order_position);
			this._data['order_position'] = order_position;
			this._isSave = true;
		}
		public function get order_position():*{ return this._order_position; }
		
		private var _priority:*;
		public function set priority(priority:*):void{
			this._priority = UseUtil.checkSetData('priority', priority);
			this._data['priority'] = priority;
			this._isSave = true;
		}
		public function get priority():*{ return this._priority; }
		
		private var _style:*;
		public function set style(style:*):void{
			this._style = UseUtil.checkSetData('style', style);
			this._data['style'] = style;
			this._isSave = true;
		}
		public function get style():*{ return this._style; }
		
		private var _target:*;
		public function set target(target:*):void{
			this._target = UseUtil.checkSetData('target', target);
			this._data['target'] = target;
			this._isSave = true;
		}
		public function get target():*{ return this._target; }
		
		private var _total_itens:*;
		public function set total_itens(total_itens:*):void{
			this._total_itens = UseUtil.checkSetData('total_itens', total_itens);
			this._data['total_itens'] = total_itens;
			this._isSave = true;
		}
		public function get total_itens():*{ return this._total_itens; }
		
		public function get isSave():Boolean{ return this._isSave; }
		public function get useTableAlias():Boolean{ return this._useTableAlias; }
	}
}

